package com.srd.phone.utils.wheel.widget;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.shengrongdai.srd.R;
import com.srd.phone.utils.wheel.adapter.ArrayWheelAdapter;

/**
 * 2016 - 05- 16 2
 * */
public class TwinningDialog extends BaseDialog implements View.OnClickListener, OnWheelChangedListener {


    private Context mContext;
    private WheelView wv_province, wv_city;

    private OnAddressCListener mOnAddressCListener;

    public TwinningDialog(Context context) {
        super(context, R.style.dialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_dialog_addr);

        setUpViews();
        setUpListener();
        setUpData();

    }

    public static TwinningDialog makeTwinningDialog(Context context, OnAddressCListener onAddressCListener){
        TwinningDialog dialog = new TwinningDialog(context);
        dialog.mContext = context;
        dialog.mOnAddressCListener = onAddressCListener;
        return dialog;
    }

    private void setUpViews(){
        wv_province = (WheelView) findViewById(R.id.wv_province);
        wv_city = (WheelView) findViewById(R.id.wv_city);
        findViewById(R.id.btn_myinfo_sure).setOnClickListener(this);
        findViewById(R.id.btn_myinfo_cancel).setOnClickListener(this);

    }

    private void setUpListener() {
        wv_province.addChangingListener(this);
        wv_city.addChangingListener(this);
    }

    private void setUpData() {
        initDatas();
        wv_province.setViewAdapter(new ArrayWheelAdapter<String>(mContext, mProvinceDatas));
        wv_province.setVisibleItems(7);
        wv_city.setVisibleItems(7);
        updateCities();
    }

    private void updateCities() {
        int pCurrent = wv_province.getCurrentItem();
        mCurrentProviceName = mProvinceDatas[pCurrent];
        String[] cities = mCitisDatasMap.get(mCurrentProviceName);
        if (cities == null) {
            cities = new String[] { "" };
        }
        wv_city.setViewAdapter(new ArrayWheelAdapter<String>(mContext, cities));
        wv_city.setCurrentItem(0);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_myinfo_sure:
                if (mOnAddressCListener != null)
                    mOnAddressCListener.onClick(mCurrentProviceName, mCurrentCityName);
                dismiss();
                break;

            case R.id.btn_myinfo_cancel:
                dismiss();
                break;
        }
    }

    @Override
    public void dismiss(){
        mContext = null;
        super.dismiss();
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == wv_province) {
            updateCities();
        } else if (wheel == wv_city) {
            mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[newValue];
        }
    }

    public interface OnAddressCListener {
        public void onClick(String provinceName, String cityName);
    }
}
